<template>
  <div class="selector-wrap disf dirc h">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.keywords"
            placeholder="关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="flex1 h0">
      <el-table height="100%" :data="filteredOrders">
        <el-table-column type="selection" width="55" align="center" v-if="!operation" />
        <el-table-column label="订单号" prop="orderNo" width="150" />
        <el-table-column label="客户名称" prop="customerName" width="150" />
        <el-table-column label="状态" align="center" width="80" prop="status">
          <template #default="scope">
            {{ GoodsOrderStatus_LABEL[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column label="总金额" align="center" width="80" prop="totalPrice" />
        <el-table-column label="创建时间" prop="createTime" width="150" />
        <el-table-column label="收件人" align="center" width="80" prop="deliveryName" />
        <el-table-column label="收件电话" align="center" width="150" prop="deliveryPhone" />
        <el-table-column label="收件地址" align="center" min-width="150" prop="deliveryAddress" />
        <el-table-column fixed="right" align="center" label="操作" width="100" v-if="operation">
          <template #default="scope">
            <el-button
              v-if="operation == 'add'"
              type="primary"
              size="small"
              icon="plus"
              @click="handleAdd(scope.row)"
            >
              添加
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination
      v-if="total > 0"
      v-model:total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      style="height: 50px"
      @pagination="handleQuery"
    />
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from "vue";
import GoodsOrderAPI, {
  GoodsOrderPageQuery,
  GoodsOrderPageVO,
  GoodsOrderStatus_LABEL,
} from "@/api/goods/goodsOrder.api";

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => [],
  },
  exQuery: {
    type: Object,
    default: () => ({}),
  },
  // 操作类型 默认 无-勾选 add-添加数量
  operation: {
    type: String,
    default: "",
  },
});
const queryFormRef = ref();
const queryParams = reactive<GoodsOrderPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const emit = defineEmits(["update:modelValue", "selection-change", "add-item"]);
const checked = ref<GoodsOrderPageVO[]>([]);

function getChecked(): GoodsOrderPageVO[] {
  return checked.value || [];
}

const total = ref(0);
const filteredOrders = ref<GoodsOrderPageVO[]>();

// 添加按钮
const handleAdd = (row: GoodsOrderPageVO) => {
  ElMessage.success("添加成功");
  emit("add-item", row);
};

const handleQuery = () => {
  Object.assign(queryParams, props.exQuery);
  GoodsOrderAPI.getPage(queryParams).then((data) => {
    filteredOrders.value = data.list;
    total.value = data.total;
  });
};

const handleResetQuery = () => {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
};

defineExpose({
  getChecked,
  handleQuery,
});
</script>

<style scoped>
.selector-wrap {
  padding: 10px;
}
</style>
